#include "List.h"
#include <iostream>

using namespace std;

List::List(int size) 
{
	m_iSize = size;
	m_pList = new int[size];
	m_iLength = 0;
}

List::~List() 
{
	delete []m_pList;
	m_pList = NULL;
}

void List::ClearList()
{
	m_iLength = 0;
}

bool List::ListEmpty()
{
	return m_iLength == 0? true:false;
}

int List::ListLength()
{
	return m_iLength;
}

bool List::GetElem(int index, int *e)
{
	if (index < 0 || index >= m_iSize)
	{
		return false;
	}

	*e = m_pList[index];
	return true;
}

int List::LocateElem(int *e)
{
	for (int i = 0; i < m_iSize; ++i)
	{
		if (m_pList[i] == *e)
		{
			return i;
		}
	}

	return -1;
}

bool List::PriorElem(int *currentElem, int *preElem)
{
	int tmp = LocateElem(currentElem);
	if (tmp == -1)
	{
		return false;
	} else {
		if (tmp == 0)
		{
			return false;
		} else {
			*preElem = m_pList[tmp - 1];
			return true;
		}
	}

	return false;
}

bool List::NextElem(int *currentElem, int *preElem)
{
	int tmp = LocateElem(currentElem);
	if (tmp == -1)
	{
		return false;
	} else {
		if (tmp == (m_iLength - 1))
		{
			return false;
		} else {
			*preElem = m_pList[tmp+1];
			return true;
		}
	}

	return false;
}

void List::ListTraverse()
{
	for (int i = 0; i < m_iLength; ++i)
	{
		cout << m_pList[i]	<< endl;	
	}
}

bool List::ListInsert(int index, int *e)
{
	if (index < 0 || index > m_iLength)
	{
		return false;
	}

	for (int k = (m_iLength - 1); k >= index; --k)
	{
		m_pList[k + 1] = m_pList[k];
	}

	m_pList[index] = *e;

	m_iLength ++;
}

bool List::ListDelete(int index, int *e)
{
	if (index < 0|| index >= m_iLength)
	{
		return false;
	}

	*e = m_pList[index];

	for (int k = index + 1; k < m_iLength; ++k)
	{
		m_pList[k - 1] = m_pList[k];
	}

	m_iLength--;
	return true;
}